/**
 * @FileName: popupEditSimpleView.js 
 * @Summary: 弹出选择框 
 * @Encoding: UTF-8 
 * @Author shixy 
 * @Version: 1.0 
 * @Date: 2009-5-15
 */
Gfa.form.QuickSearchField = function(config){
	Gfa.form.QuickSearchField.superclass.constructor.call(this,config);
	this.addEvents({
		"afterSelect" : true
	});
	this.on("afterSelect", this.fillText, this);
};
Ext.extend(Gfa.form.QuickSearchField,Ext.form.TriggerField,{
	triggerClass : 'x-form-search-trigger',
	displayField : null,
	valueField : null,
	onTriggerClick : function(){
		if(this.disabled){
			return;
		}
		var qsd = new Gfa.form.QuickSearchDialog(this);
		qsd.showDialog();
	},
	fillText : function(entity){
		this.setValue(entity[this.valueField]);
	}
});
Ext.reg('gfaquicksearchfield',Gfa.form.QuickSearchField);



Gfa.form.QuickSearchDialog = function(config){
	this.QuickField = config;
	Gfa.form.QuickSearchDialog.superclass.constructor.call(this,config);
}
Ext.extend(Gfa.form.QuickSearchDialog,Ext.BoxComponent,{
	/**
	 * @config 各项属性配置
	 * @para fieldLabel 文本框显示
	 * @para fieldName 文本框字段
	 * @para colWidth 列表中对应列的长度
	 * @para dataType 字段数据类型，当为‘String’时查询条件为前匹配‘likeeic’，其他为‘eq’
	 * @para displayInList 是否在列表显示
	 * @para displayInQuery 是否作为查询条件
	 */
	entityConfig : null,
	/**
	 * @config 窗口标题
	 */
	title : null,
	/**
	 * @config (int)页面显示数据条数 默认为15
	 */
	pageSize : 15,
	/**
	 * @config 附加查询条件 
	 */
	whereCondition : null,
	/**
	 * @config 是否可多选，多选的话grid会显示复选框，默认为单选
	 */
	multiSelect : false,
	/**
	 * @config 窗口高度 默认为400
	 */
	win_height : 400,
	/**
	 * @config 窗口宽度 默认为600
	 */
	win_width : 600,
	
	/**
	 * @config 初始化时(打开页面后)是否查询数据 默认为true
	 */
	initQueryFlag : true,
	/**
	 * @config 初始化窗体，创建窗口中的内容，添加相关监听事件
	 */
	initElement : function(){
		this.topPanel = this.createQueryCmp();
		this.cm = Gfa.GridHelper.buildColumnModel(this.entityConfig, this,true);
		var length = this.entityConfig.fields.length
		for(var i=0;i<length;i++){
			//取i+1是由于每个列表框中序号占了一列
			this.cm.setColumnWidth(i+1,this.entityConfig.fields[i].colWidth);
		};
		this.reader = Gfa.GridHelper.buildReader(this.entityConfig);
		if (this.multiSelect == true)
			this.sm = new Ext.grid.CheckboxSelectionModel({
				singleSelect : false
			});
		else
			this.sm = new Ext.grid.RowSelectionModel({
				singleSelect : true
			});
		
		this.store = new Gfa.data.Store({
			proxy : new Gfa.data.DwrProxy(CommonService),
			reader : this.reader
		});
		this.store.on('beforeload', function(store, options) {
			if (options.params === undefined) {
				options.params = {};
			}
			if (options.params.start === undefined) {
				options.params.start = 0;
			}
			var queryInfo = Gfa.QueryInfoHelper.buildQueryInfo(options.params,
					this.topPanel, {
						queryObjectName : this.entityConfig.className
					});
			queryInfo.whereCondition = this.whereCondition;
			store.proxy.DWR = CommonService;
			options.params.dwrMethod = "findByQueryInit";
			options.params.dwrArguments = [queryInfo, []];
		}, this);
		
		this.grid = new Ext.grid.EditorGridPanel({
				store : this.store,
				cm : this.cm,
				tbar : this.topPanel,
				bbar : new Ext.PagingToolbar({
					store : this.store,
					displayInfo : true,
					beforePageText : i18n.message.beforePageText,
					afterPageText : i18n.message.afterPageText,
					displayMsg : i18n.message.displayMsg,
					pageSize : this.pageSize
				}),
				sm : this.sm,
				border : false,
				autoScroll : true,
				loadMask : true,
				anchor : '0 0'
			});
		this.grid.on('rowdblclick',this.setSelectedData,this);	
	},
	/**
	 * @summary 创建窗口
	 */
	createWindow : function(){
		this.initElement();
		this.window = new Ext.Window({
			title : this.title,
			height : this.win_height,
			width : this.win_width,
			layout : 'fit',
			closeAction : 'hide',
			plain : true,
			modal : true,
			items : [this.grid]
		});
	},
	/**
	 * @summary 显示窗口
	 */
	showDialog : function(){
		this.createWindow();
		this.window.show();
		if(this.initQueryFlag){
			this.store.load();
		}
	},
	/**
	 * @summary 创建查询区域
	 */
	createQueryCmp : function(){
		var items = [];
		if (this.entityConfig.fields != undefined
				&& this.entityConfig.fields != null) {
			for (var i = 0; i < this.entityConfig.fields.length; i++) {
				var fieldDefination = this.entityConfig.fields[i];
				if (fieldDefination.displayInQuery == false)
					continue;
				var item = {};
				item.layout = 'form';
				item.bodyStyle = 'padding:1px 1px 0';
				item.labelWidth = 80;
				item.border = false;
				item.items = {};
				item.items.fieldLabel = fieldDefination.fieldLabel;
				item.items.name = fieldDefination.fieldName;
				item.items.xtype = 'textfield';
				item.items.anchor = '-10';
				item.items.queryField = {
					condition : Gfa.GlobalConstant.CONDITION_EQ
				};
				if (fieldDefination.dataType == 'java.util.Date')
					item.items.xtype = 'datefield';
				if (fieldDefination.dataType == 'String')
					item.items.queryField = {
						condition : Gfa.GlobalConstant.CONDITION_LIKE_END_IC
					};
				items.push(item);
			}
		}
		if (items.length == 0)
			return;
		this.toolbar = new Ext.Toolbar({
			items : ['-',{
				text : i18n.global.search,
				iconCls : 'toolbar-search',
				handler : function() {
					this.store.load()
				},
				scope : this
			}, {
				text : i18n.global.reset,
				iconCls : 'toolbar-reset',
				scope : this
			}, '-',{
		    	text : i18n.global.select,
				iconCls : 'toolbar-save',
		    	handler : this.setSelectedData,
		    	scope : this
	    	}]
		});
		return new Ext.form.FormPanel({
			bodyStyle : 'padding:5px 5px 0',
			cls : 'gfa-form',
			layoutConfig : {
				columns : 3
			},
			defaults : {
				width : 260
			},
			layout : 'table',
			border : false,
			autoScroll : true,
			bbar : this.toolbar,
			items : items
		});
	},
	setSelectedData : function(){
		if(this.grid){
			var rec = this.grid.getSelectionModel().getSelected();
			if(rec == null){
				Ext.MessageBox.alert("提示", "请选择一条记录");
				return;
			};
			this.entity = rec.data;
			this.QuickField.fireEvent("afterSelect", this.entity);
			this.window.hide();
		}
		
	}
});